package com.example.tellwin.utils;

import android.content.Context;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.tellwin.R;

/**
 * Toast工具类
 *
 * @author : randymchen
 * @date : 2019.08.09
 */
public class ToastUtil {

    private static Toast mToast;
    private static TextView mText;
    private static volatile ToastUtil instance;
    private UIHandler mUIHandler;

    private ToastUtil(Context context) {
        mUIHandler = new UIHandler();
        mUIHandler.post(new Runnable() {
            @Override
            public void run() {
                initToast(context);
            }
        });
    }

    private void initToast(Context context) {
        if (mToast == null) {
            mToast = new Toast(context.getApplicationContext());
            View view = LayoutInflater.from(context.getApplicationContext()).inflate(R.layout.layout_toast, null);
            mText = view.findViewById(R.id.toast_text);
            mToast.setView(view);
            mToast.setGravity(Gravity.CENTER, 0, 0);
        }
    }

    public static void show(Context context, int resId) {
        show(context, context.getResources().getString(resId), Toast.LENGTH_SHORT);
    }

    public static void show(Context context, CharSequence text) {
        show(context, text, Toast.LENGTH_SHORT);
    }

    public static void show(Context context, int resId, int duration) {
        show(context, context.getResources().getString(resId), duration);
    }

    public static void show(Context context, int resId, Object... args) {
        show(context, String.format(context.getResources().getString(resId), args), Toast.LENGTH_SHORT);
    }

    public static void show(Context context, String format, Object... args) {
        show(context, String.format(format, args), Toast.LENGTH_SHORT);
    }

    public static void show(Context context, int resId, int duration, Object... args) {
        show(context, String.format(context.getResources().getString(resId), args), duration);
    }

    public static void show(Context context, String format, int duration, Object... args) {
        show(context, String.format(format, args), duration);
    }

    public static void show(Context context, CharSequence text, int duration) {
        getInstance(context).showToast(context, text, duration);
    }

    public static ToastUtil getInstance(Context context) {
        if (instance == null) {
            synchronized (ToastUtil.class) {
                if (instance == null) {
                    instance = new ToastUtil(context);
                }
            }
        }
        return instance;
    }

    private void showToast(Context context, CharSequence text, int duration) {
        mUIHandler.post(new Runnable() {
            @Override
            public void run() {
                if (Build.VERSION.SDK_INT == Build.VERSION_CODES.P) { //安卓9.0，同一个toast多次show导致不显示toast,源码改动增加record.callback.hide()，导致无法立马显示
                    if (mToast != null) {
                        mToast.cancel();
                    }
                    mToast = null;
                }
                initToast(context);
                mText.setText(text);
                if (mToast != null) {
                    mToast.setDuration(duration);
                    mToast.show();
                }
            }
        });
    }

    public void cancelToast() {
        if (mToast != null) {
            mToast.cancel();
            mToast = null;
            mText = null;
        }
        mUIHandler.removeCallbacksAndMessages(null);
    }
}
